/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.model.sap;

import com.bifrost.entity.OrderItem;
import com.bifrost.entity.OrderReturnItem;
import com.bifrost.entity.SapStoreGoods;
import com.bifrost.entity.TransactionSendLog;
import com.thoughtworks.xstream.annotations.XStreamAlias;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * 销售行项目
 *
 * @author joewee
 * @version 1.0.0
 * @Date 2018/5/18 12:00
 */
@XStreamAlias("ITEM2")
public class RetailItem extends BaseXml implements Serializable {
    public static final String RETAIL_TYPE_NULL_REFUND = "1801";
    public static final String RETAIL_TYPE_NULL_SALES = "1001";

    public static final String RETAIL_TYPE_4_REFUND = "1803";
    public static final String RETAIL_TYPE_4_SALES = "1003";

    public static final String RETAIL_TYPE_6_REFUND = "1804";
    public static final String RETAIL_TYPE_6_SALES = "1004";

    /**
     * 行号
     */
    @XStreamAlias("RETAILNUMBER")
    private Integer retailNumber;
    /**
     * 销售项目类型
     */
    @XStreamAlias("RETAILTYPECODE")
    private String retailTypeCode;
    /**
     * 商品编码
     */
    @XStreamAlias("ITEMID")
    private String itemId;
    /**
     * 销售数量
     */
    @XStreamAlias("RETAILQUANTITY")
    private Integer retailQuantity;
    /**
     * 销售金额
     */
    @XStreamAlias("SALESAMOUNT")
    private BigDecimal salesAmount;
    /**
     * 原小票号，默认0
     */
    @XStreamAlias("SERIALNUMBER")
    private String serialNumber = "0";
    /**
     * 促销号，默认为0
     */
    @XStreamAlias("PROMOTIONID")
    private String promtionId = "0";
    /**
     * 物权公司代码，默认为空
     */
    @XStreamAlias("ENTRYMETHODCODE")
    private String entryMethodCode = "";
    /**
     * 实际零售价
     */
    @XStreamAlias("ACTUALUNITPRICE")
    private BigDecimal actualUnitPrice;


    /**
     *
     * @param o OrderItem或者OrderReturnItem
     * @param storeGoods
     * @param retailNumber 以order_item_id排序取出来的行项目次序
     */
    /*public <T> RetailItem(T o, SapStoreGoods storeGoods,Integer retailNumber) {
        this.retailNumber = retailNumber;
        if(o instanceof OrderItem) {
            OrderItem item  = (OrderItem)o;
            this.retailTypeCode = getRetailTypeCode(storeGoods.getConsProcg(),false);
            this.itemId = item.getExternalId();
            this.retailQuantity = item.getSkuQuantity();
            this.salesAmount = item.getSkuPrice().multiply(new BigDecimal(item.getSkuQuantity()));
            this.actualUnitPrice = item.getSkuPrice();
        } else if(o instanceof OrderReturnItem) {
            OrderReturnItem item  = (OrderReturnItem)o;
            this.retailTypeCode = getRetailTypeCode(storeGoods.getConsProcg(),true);
            this.itemId = item.getExternalId();
            this.retailQuantity = item.getSkuQuantity();
            this.salesAmount = item.getSkuPrice().multiply(new BigDecimal(item.getSkuQuantity()));
            this.actualUnitPrice = item.getSkuPrice();
        } else {
            throw new IllegalArgumentException("参数：" + o + "类型不正确" + "期望的类型是OrderItem或者OrderReturnItem");
        }


    }*/

    /**
     * @param o            OrderItem或者OrderReturnItem
     * @param storeGoods
     * @param retailNumber 以order_item_id排序取出来的行项目次序
     */
    public <T> RetailItem(T o, SapStoreGoods storeGoods, Integer retailNumber, TransactionSendLog.Type type) {
        this.retailNumber = retailNumber;
        if (o instanceof OrderItem) {
            OrderItem item = (OrderItem) o;
            this.retailTypeCode = getRetailTypeCode(storeGoods.getConsProcg(), TransactionSendLog.Type.SALES.equals(type));
            this.itemId = item.getExternalId();
            this.retailQuantity = item.getSkuQuantity();
            this.salesAmount = item.getSkuPrice().multiply(new BigDecimal(item.getSkuQuantity()));
            this.actualUnitPrice = item.getSkuPrice();
        } else if (o instanceof OrderReturnItem) {
            OrderReturnItem item = (OrderReturnItem) o;
            this.retailTypeCode = getRetailTypeCode(storeGoods.getConsProcg(), TransactionSendLog.Type.SALES.equals(type));
            this.itemId = item.getExternalId();
            this.retailQuantity = item.getSkuQuantity();
            this.salesAmount = item.getSkuPrice().multiply(new BigDecimal(item.getSkuQuantity()));
            this.actualUnitPrice = item.getSkuPrice();
        } else {
            throw new IllegalArgumentException("参数：" + o + "类型不正确" + "期望的类型是OrderItem或者OrderReturnItem");
        }


    }

    /**
     * @param consProcg
     * @param b         是否是正向销售
     * @return
     */
    private String getRetailTypeCode(String consProcg, boolean b) {
        if (null == consProcg) {
            if (b) {
                return RETAIL_TYPE_NULL_SALES;
            } else {
                return RETAIL_TYPE_NULL_REFUND;
            }

        }
        if ("4".equals(consProcg)||"3".equals(consProcg)) {
            if (b) {
                return RETAIL_TYPE_4_SALES;
            } else {
                return RETAIL_TYPE_4_REFUND;
            }

        }
        if ("6".equals(consProcg)) {
            if (b) {
                return RETAIL_TYPE_6_SALES;
            } else {
                return RETAIL_TYPE_6_REFUND;
            }

        }
        return "";
    }

    public RetailItem() {

    }

    public Integer getRetailNumber() {
        return retailNumber;
    }

    public void setRetailNumber(Integer retailNumber) {
        this.retailNumber = retailNumber;
    }

    public String getRetailTypeCode() {
        return retailTypeCode;
    }

    public void setRetailTypeCode(String retailTypeCode) {
        this.retailTypeCode = retailTypeCode;
    }

    public String getItemId() {
        return itemId;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    public Integer getRetailQuantity() {
        return retailQuantity;
    }

    public void setRetailQuantity(Integer retailQuantity) {
        this.retailQuantity = retailQuantity;
    }

    public BigDecimal getSalesAmount() {
        return salesAmount;
    }

    public void setSalesAmount(BigDecimal salesAmount) {
        this.salesAmount = salesAmount;
    }

    public String getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    public String getPromtionId() {
        return promtionId;
    }

    public void setPromtionId(String promtionId) {
        this.promtionId = promtionId;
    }

    public String getEntryMethodCode() {
        return entryMethodCode;
    }

    public void setEntryMethodCode(String entryMethodCode) {
        this.entryMethodCode = entryMethodCode;
    }

    public BigDecimal getActualUnitPrice() {
        return actualUnitPrice;
    }

    public void setActualUnitPrice(BigDecimal actualUnitPrice) {
        this.actualUnitPrice = actualUnitPrice;
    }
}
